import { Component, OnInit, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Params, ActivatedRoute } from '@angular/router';
import { DatePipe } from '@angular/common';
import { ExportFileService } from '../../../core/exportFile';
import { TableCheckPageComponent } from '../../../layout/components/tableCheckPage.component';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-safehiddendanger',
    templateUrl: './safeHiddenDanger.component.html',
    styleUrls: ['./safeHiddenDanger.component.less']
})

export class SafeHiddenDangerComponent implements OnInit {
    @ViewChild('tcp', { static: true }) tcp: TableCheckPageComponent;
    treeNodeStyle = { 'max-width': '300px', 'max-height': '400px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };
    tableScroll = { x: '3000px' };

    searchParam: any = {};
    headData = [];
    plainData = [];
    tbbmNodes = [];
    zrbmNodes = [];
    phbmNodes = [];
    dbbmNodes = [];
    yhdjSelect = [];
    ztSelect = [];
    operaList: any = [
        { addModal: false },
        { editModal: false },
        { delModal: false },
        { sqxhModal: false },
        { pzModal: false },
        { bhModal: false },
        { sqrkModal: false },
        { pzrkModal: false },
        { bhrkModal: false },
        { zgwtModal: false }
    ];
    tempData = [];
    apiUrl: any = {};
    exportUrl = '';

    constructor(
        private http: HttpClient,
        private activatedRoute: ActivatedRoute,
        private exportFile: ExportFileService,
        private datePipe: DatePipe
    ) { }

    ngOnInit() {
        this.headData = [
            { "name": "提报部门", "width": "4%" },
            { "name": "隐患名称", "width": "4%" },
            { "name": "发现时间", "width": "4%" },
            { "name": "是否销号", "width": "4%" },
            { "name": "问题来源", "width": "4%" },

            { "name": "隐患等级", "width": "4%" },
            { "name": "隐患状况", "width": "8%" },
            { "name": "原因分析", "width": "8%" },
            { "name": "整治措施", "width": "8%" },
            { "name": "整治目标或预期成效", "width": "" },

            { "name": "责任部门", "width": "4%" },
            { "name": "配合部门", "width": "4%" },
            { "name": "督办部门", "width": "4%" },
            { "name": "整改时限", "width": "4%" },
            { "name": "牵头领导", "width": "4%" },

            { "name": "整改进展情况", "width": "8%" },
            { "name": "审批人", "width": "4%" },
            { "name": "审批时间", "width": "4%" },
            { "name": "审批意见", "width": "4%" }
        ];
        this.plainData = [
            "submitOrgName", "problemName", "discoverDate", "statusName", "source",
            "problemLevel", "problemDesc", "reason", "measure", "goal",
            "chargeOrgName", "assistOrgName", "superviceOrgName", "deadline", "leader",
            "handleInfo", "addApprUser", "addApprTime", "addApprAdvice"
        ];
        this.http.get(environment.apiBase + '/csmis/getKmnOrgTree.jwt?showAll=1&showSeg=1').subscribe((res: any) => {
            Object.assign(this.tbbmNodes, res);
            Object.assign(this.zrbmNodes, res);
            Object.assign(this.phbmNodes, res);
            Object.assign(this.dbbmNodes, res);
        });
        this.yhdjSelect = ["一般", "突出", "重大"];
        this.ztSelect = [
            { "value": "0", "label": "编辑中" },
            { "value": "1", "label": "申请入库" },
            { "value": "2", "label": "已入库" },
            { "value": "3", "label": "入库驳回" },
            { "value": "4", "label": "申请销号" },
            { "value": "5", "label": "销号驳回" },
            { "value": "6", "label": "已销号" }
        ];
        this.apiUrl.add = environment.project + '/safeHideProblem/add';
        this.apiUrl.edit = environment.project + '/safeHideProblem/update';
        this.apiUrl.del = environment.project + '/safeHideProblem/delete';
        this.apiUrl.sqxh = environment.project + '/safeHideProblem/applyCancle';
        this.apiUrl.pz = environment.project + '/safeHideProblem/approveCancle';
        this.apiUrl.bh = environment.project + '/safeHideProblem/rejectCancle';
        this.apiUrl.sqrk = environment.project + '/safeHideProblem/applyAdd';
        this.apiUrl.pzrk = environment.project + '/safeHideProblem/approveAdd';
        this.apiUrl.bhrk = environment.project + '/safeHideProblem/rejectAdd';
        this.apiUrl.zgwt = environment.project + '/safeHideProblem/handle';
        this.loadList();
        // 参数有ID表示从问题库转过来的
        this.activatedRoute.queryParams.subscribe((params: Params) => {
            if (params.id) {
                this.modalShow('addModal');
            }
        });
    }
    loadList() {
        this.tcp.loadList(environment.project + '/safeHideProblem/query', '');
    }
    search() {
        let params = '';
        for (const item of Object.keys(this.searchParam)) {
            const data = this.searchParam[item];
            if (data || data === 0) {
                params += '&' + item + '=';
                if (data instanceof Date) {
                    params += this.datePipe.transform(data, 'yyyy-MM-dd');
                } else {
                    params += data;
                }
            }
        }
        this.exportUrl = params;
        this.tcp.loadList(environment.project + '/safeHideProblem/query', params);
    }
    export() {
        this.exportFile.getDown(environment.project + '/safeHideProblem/export?page=1&limit=20' + this.exportUrl, '安全隐患库.xls');
    }
    modalShow(type) {
        this.tempData = this.tcp.getAllData();
        this.operaList[type] = true;
    }
    modalHide(type) {
        this.operaList[type] = false;
    }
}
